/**
 * 
 */
package com.hw.hcit.feature;


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import com.hw.hcit.R;
import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

/**
 * @author gaoshengpan
 *
 */
public class GPSTestItem extends JTestItem {
   private TextView tvSatelite[];
   private Location m_location;
   private LocationManager m_mgr;
   private GpsStatus.Listener m_statusListener;
   private GpsStatus.Listener statusListener;
   private TextView mtv_LocationSuccess;
   private TextView mtv_Testtime;
   Timer st_timer;
   private String mStrAzimuth;
   private String mStrElevation;
   private String mStrPrn;
   private String mStrSnr;
   PowerManager.WakeLock wakeLock;
   boolean m_isGpsOpen;
   private static final int MAX_SATELITE_COUNT = 10;
   GpsInfo mGpsInfo[] = new GpsInfo[30];
   GpsInfo mGpsInfo_sort[];
   private int mStateliteCount;
   private GpsStatus m_gpsStatus;
   Handler hGpsHand;
   private int mSecond;
   private LocationListener locationListener;
   private boolean tested = false;
   private boolean powerClicked = false;
   private String TAG = "GPSTestItem";
   private Button mButton1,mButton2;

   class GpsInfo {

      private float fAzimuth;
      private float fElevation;
      int iID;
      int prn;
      private float snr;
      boolean sort;
      GPSTestItem GPSTestItem;

      public GpsInfo() {
         GPSTestItem = GPSTestItem.this;
         prn = 0;
         iID = 0;
         fAzimuth = 0F;
         fElevation = 0F;
         snr = 0F;
      }
   }

   class st_TimerTask extends TimerTask {

      public void run() {
         hGpsHand.sendEmptyMessage(0);

      }

   }

   /**
    * @param cb
    */
   public GPSTestItem(JCtrlCallback cb) {
      super(cb);
      // TODO Auto-generated constructor stub

   }

   public GPSTestItem() {
      // TODO Auto-generated constructor stub
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
	  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  Date curDate = new Date(System.currentTimeMillis());
	  String str = formatter.format(curDate);
	  storeToFile(str);
	  storeToFile("BEGIN TEST");
	  /*15-09-02 gaoshenpan fix init array when oncreat begin*/
	  GpsInfo agpsinfo_sort[] = new GpsInfo[30];
      for (int i = 0; i < 10; i++) {
         mGpsInfo[i] = new GpsInfo();
         Log.d(TAG, "mGpsInfo------>notnull" + i);
      }
      Log.d(TAG, "mGpsInfo.size------>" + mGpsInfo.length);
      mGpsInfo_sort = agpsinfo_sort;
      /*15-09-02 gaoshenpan fix init array when oncreat begin*/
      m_mgr = (LocationManager) Config.GetActivity().getSystemService(
            "location");
      hGpsHand = new GHandler();
      if (!m_mgr.isProviderEnabled("gps")) {
         m_isGpsOpen = true;
         openGPS();
      }
   }
   /*15-10-27 gaoshenpan add method setSatelliteInfo begin*/
   public void setSatelliteInfo(){
	   Iterator iterator = m_mgr.getGpsStatus(null).getSatellites().iterator();
	      int k = 0;
	      String s5;
	      for (; iterator.hasNext(); Log.d(TAG, s5)) {
	         GpsSatellite gpssatellite = (GpsSatellite) iterator.next();
	         GpsInfo gpsinfo = mGpsInfo[k];
	         int l = gpssatellite.getPrn();
	         gpsinfo.prn = l;
	         GpsInfo gpsinfo1 = mGpsInfo[k];
	         float f = gpssatellite.getAzimuth();
	         gpsinfo1.fAzimuth = f;
	         GpsInfo gpsinfo2 = mGpsInfo[k];
	         float f1 = gpssatellite.getElevation();
	         gpsinfo2.fElevation = f1;
	         GpsInfo gpsinfo3 = mGpsInfo[k];
	         float f2 = gpssatellite.getSnr();
	         gpsinfo3.snr = f2;
	         mGpsInfo[k].iID = k;
	         k++;
	         if (mGpsInfo[k] == null) {
	            Log.d(TAG, "mGpsInfo[k]---1--->k" + k);
	            break;
	         }
	         StringBuilder stringbuilder = (new StringBuilder())
	               .append("mGpsInfo[iCount].prn is ");
	         int i1 = mGpsInfo[k].prn;
	         String s1 = stringbuilder.append(i1).toString();
	         Log.d(TAG, s1);
	         StringBuilder stringbuilder1 = (new StringBuilder())
	               .append("mGpsInfo[iCount].fAzimuth is ");
	         float f3 = mGpsInfo[k].fAzimuth;
	         String s2 = stringbuilder1.append(f3).toString();
	         Log.d(TAG, s2);
	         StringBuilder stringbuilder2 = (new StringBuilder())
	               .append("mGpsInfo[iCount].fElevation");
	         float f4 = mGpsInfo[k].fElevation;
	         String s3 = stringbuilder2.append(f4).toString();
	         Log.d(TAG, s3);
	         StringBuilder stringbuilder3 = (new StringBuilder())
	               .append("mGpsInfo[iCount].snr");
	         float f5 = mGpsInfo[k].snr;
	         String s4 = stringbuilder3.append(f5).toString();
	         Log.d(TAG, s4);
	         StringBuilder stringbuilder4 = (new StringBuilder())
	               .append("mGpsInfo[iCount].iID");
	         int j1 = mGpsInfo[k].iID;
	         s5 = stringbuilder4.append(j1).toString();
	      }

	      mStateliteCount = k;
	      Log.d(TAG, " k=" + k);
	      StringBuilder stringbuilder5 = (new StringBuilder())
	            .append("the mStateliteCount is");
	      int k1 = mStateliteCount;
	      String s6 = stringbuilder5.append(k1).toString();
	      Log.d(TAG, s6);
	      sortStateliteinfo(k);
	      Log.d(TAG,"sortsetStateliteinfo--->setSatelliteInfo1");
	      sortsetStateliteinfo(k);
	      Log.d(TAG,"sortsetStateliteinfo--->setSatelliteInfo2");
   }
   
   /*15-10-27 gaoshenpan add method setSatelliteInfo end*/
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View rootView = inflater.inflate(R.layout.gps_test, container, false);
      // mGpsInfo[] = new GpsInfo[10] ;
      Log.d(TAG,"onCreateView--->");
      tvSatelite = new TextView[10];
      Log.d(TAG,"onCreateView--->"+"1");
      tvSatelite[0] = (TextView) rootView.findViewById(R.id.tv_st_1);
      tvSatelite[1] = (TextView) rootView.findViewById(R.id.tv_st_2);
      tvSatelite[2] = (TextView) rootView.findViewById(R.id.tv_st_3);
      tvSatelite[3] = (TextView) rootView.findViewById(R.id.tv_st_4);
      tvSatelite[4] = (TextView) rootView.findViewById(R.id.tv_st_5);
      tvSatelite[5] = (TextView) rootView.findViewById(R.id.tv_st_6);
      tvSatelite[6] = (TextView) rootView.findViewById(R.id.tv_st_7);
      tvSatelite[7] = (TextView) rootView.findViewById(R.id.tv_st_8);
      tvSatelite[8] = (TextView) rootView.findViewById(R.id.tv_st_9);
      tvSatelite[9] = (TextView) rootView.findViewById(R.id.tv_st_10);
      Log.d(TAG,"onCreateView--->"+"10");
      mButton1 = (Button) rootView.findViewById(R.id.button1);
		mButton2 = (Button) rootView.findViewById(R.id.button2);
		mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(26, 'P');
					} else {
						rewriteOneByteOfNv2499(57, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(26, 'F');
					} else {
						rewriteOneByteOfNv2499(57, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});

      mtv_LocationSuccess = (TextView) rootView
            .findViewById(R.id.tv_st_success);
      mtv_LocationSuccess.setText(R.string.tv_st_success);
      mtv_Testtime = (TextView) rootView.findViewById(R.id.tv_st_time);
      mStrSnr = getString(R.string.gps_st_info_snr);
      mStrPrn = getString(R.string.gps_st_info_prn);
      mStrAzimuth = getString(R.string.gps_st_info_azimuth);
      mStrElevation = getString(R.string.gps_st_info_elevation);
      return rootView;
   }

   @Override
   public void onDestroy() {
      // TODO Auto-generated method stub
      super.onDestroy();
      storeToFile("END THE TEST !!!");
   }

   private void openGPS() {
      android.provider.Settings.Secure.setLocationProviderEnabled(Config
            .GetActivity().getContentResolver(), "gps", true);
   }

   private void closeGPS() {
      android.provider.Settings.Secure.setLocationProviderEnabled(Config
            .GetActivity().getContentResolver(), "gps", false);
   }

   private void sortStateliteinfo(int i) {
      int j = 0, k = 0;
      for (j = 0; j < i; j++) {
         mGpsInfo[j].sort = false;
      }
      for (j = 0; j < i; j++) {
         float snr_temp = 0;
         int tag = 0;
         for (k = 0; k < i; k++) {
            if ((mGpsInfo[k].sort == false) && (mGpsInfo[k].snr > snr_temp)) {
               snr_temp = mGpsInfo[k].snr;
               tag = k;
            }
         }
         mGpsInfo_sort[j] = mGpsInfo[tag];
         mGpsInfo[tag].sort = true;
      }
   }

   private void sortsetStateliteinfo(int i) {
      int j = i;
      Log.d(TAG,"sortsetStateliteinfo--->"+i);
      if (j > 10)
         j = 10;
      for (int k = 0; k < j; k++) {
    	  if(tvSatelite[k] == null){
    		  Log.d(TAG,"sortsetStateliteinfo--->"+k+"=null")  ;
    	  }
         TextView textview = tvSatelite[k];
         StringBuilder stringbuilder = (new StringBuilder()).append("ID:");
         int l = k + 1;
         StringBuilder stringbuilder1 = stringbuilder.append(l).append("  ");
         String s = mStrSnr;
         StringBuilder stringbuilder2 = stringbuilder1.append(s);
         float f = mGpsInfo_sort[k].snr;
         StringBuilder stringbuilder3 = stringbuilder2.append(f)
               .append("  ");
         String s1 = mStrPrn;
         StringBuilder stringbuilder4 = stringbuilder3.append(s1);
         int i1 = mGpsInfo[k].prn;
         String s2 = stringbuilder4.append(i1).toString();
         textview.setText(s2);
         String s4 = "ID:" + l + "  " + "Snr:" + mGpsInfo_sort[k].snr + "  "
               + "Prn:" + mGpsInfo[k].prn + "  " + "Total:" + i;
         String s3 = "";
         if (m_location != null) {
            s3 = "Latitude:" + m_location.getLatitude() + "  "
                  + "Longitude:" + m_location.getLongitude();
         }
         storeToFile(s4+"  "+s3);
         if (k == 4 && f >= 30) {
            setSuccess();
         }
      }

   }

   public void setSuccess() {
	   tested = true;
      int j = m_gpsStatus.getTimeToFirstFix();
      mtv_LocationSuccess.setText(R.string.tv_st_located);
      /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method to stop begin*/
      hGpsHand.removeMessages(0);
//      st_timer.cancel();
      /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method to stop end*/
      mButton1.setEnabled(true);
      String s = (new StringBuilder())
            .append("GpsStatus.GPS_EVENT_FIRST_FIX the fix Time is ")
            .append(j).toString();
      Log.d(TAG, s);
   }

   private void updateWithNewLocation(Location location) {
      m_location = location;
   }

   private void setStateliteinfo(int i) {
      int j = i;
      if (j > 10)
         j = 10;
      for (int k = 0; k < j; k++) {
         TextView textview = tvSatelite[k];
         StringBuilder stringbuilder = (new StringBuilder()).append("ID:");
         int l = k + 1;
         StringBuilder stringbuilder1 = stringbuilder.append(l).append("  ");
         String s = mStrSnr;
         StringBuilder stringbuilder2 = stringbuilder1.append(s);
         float f = mGpsInfo[k].snr;
         StringBuilder stringbuilder3 = stringbuilder2.append(f)
               .append("  ");
         String s1 = mStrPrn;
         StringBuilder stringbuilder4 = stringbuilder3.append(s1);
         int i1 = mGpsInfo[k].prn;
         StringBuilder stringbuilder5 = stringbuilder4.append(i1).append(
               "\n");
         String s2 = mStrAzimuth;
         StringBuilder stringbuilder6 = stringbuilder5.append(s2).append(
               "  ");
         float f1 = mGpsInfo[k].fAzimuth;
         StringBuilder stringbuilder7 = stringbuilder6.append(f1).append(
               "  ");
         String s3 = mStrElevation;
         StringBuilder stringbuilder8 = stringbuilder7.append(s3);
         float f2 = mGpsInfo[k].fElevation;
         String s4 = stringbuilder8.append(f2).toString();
         textview.setText(s4);
      }

   }

   @Override
   public void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      tested = false;
	  powerClicked = false;
	  mButton1.setEnabled(false);
	  /* gaoshenpan 15-11-18 fix meaasgeInfo when onresume begin*/
	  mtv_LocationSuccess.setText(R.string.tv_st_success);
	  /* gaoshenpan 15-11-18 fix meaasgeInfo when onresume end*/
//      mButton2.setEnabled(false);
	  /*15-10-27 gaoshenpan fix Attempt to read from null array begin*/
	  if (!m_mgr.isProviderEnabled("gps")) {
	         m_isGpsOpen = true;
	         openGPS();
	      }
	  setSatelliteInfo();
	  /*15-10-27 gaoshenpan fix Attempt to read from null array end*/
      mSecond = 0;
      statusListener = new GpsStatusListner();
      locationListener = new LocaltionLis();
     /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method begin*/
      /* gaoshenpan 15-11-18 fix meaasge not removed when gps fixed begin*/
      hGpsHand.sendEmptyMessageDelayed(0, 1000);
      /* gaoshenpan 15-11-18 fix meaasge not removed when gps fixed end*/
      /*Timer timer = new Timer();
      st_timer = timer;
      st_TimerTask st_timertask = new st_TimerTask();
      if (st_timertask == null) {
         Log.d(TAG, "----->st_timertask == null");
      }
      if (st_timer == null) {
         Log.d(TAG, "----->st_timer == null");
      }
      st_timer.schedule(st_timertask, 100L, 1000L);*/
      /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method end*/
      boolean flag = m_mgr.addGpsStatusListener(statusListener);
      m_mgr.requestLocationUpdates("gps", 0L, 0F, locationListener);
      String s = (new StringBuilder()).append("Add the statusListner is")
            .append(flag).toString();
      Log.e(TAG, s);
      m_location = m_mgr.getLastKnownLocation(TAG);
      updateWithNewLocation(m_location);
      wakeLock = ((PowerManager) Config.GetActivity().getSystemService(
            "power")).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
            | PowerManager.ON_AFTER_RELEASE, "GPSTestItem");
      wakeLock.acquire();
   }

   @Override
   public void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method to stop begin*/
      hGpsHand.removeMessages(0);
      /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method to stop end*/
      m_mgr.removeUpdates(locationListener);
      if (wakeLock != null) {
         wakeLock.release();
      }
   }

   @Override
   public void onStop() {
      // TODO Auto-generated method stub
      super.onStop();

      m_mgr.removeGpsStatusListener(statusListener);
      /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method to stop begin*/
//    st_timer.cancel();
      hGpsHand.removeMessages(0);
      /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method to stop end*/

      if (m_isGpsOpen)
         closeGPS();
   }

   private class GHandler extends Handler {

      public void handleMessage(Message message) {
         switch (message.what) {
         case 0:
            TextView textview = mtv_Testtime;
            StringBuilder stringbuilder = (new StringBuilder()).append("");
            mSecond++;
            int i = mSecond;
            String s = stringbuilder.append(i).toString();
            textview.setText(s);
            break;
         default:
            break;
         }
         /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method begin*/
         hGpsHand.sendEmptyMessageDelayed(0, 1000);
         /* gaoshenpan 15-08-14 fix timer not stop when it is cancel replace with another method end*/

      }
   }
   

   private class GpsStatusListner implements GpsStatus.Listener {

      public void onGpsStatusChanged(int status) {
         GPSTestItem GPSTestItem = GPSTestItem.this;
         GpsStatus gpsstatus = m_mgr.getGpsStatus(null);
         GPSTestItem.m_gpsStatus = gpsstatus;
         /*
          * i; JVM INSTR tableswitch 1 4: default 56 // 1 615 // 2 626 // 3
          * 57 // 4 124; goto _L1 _L2 _L3 _L4 _L5
          */
         // _L1:
         // return;
         // _L4:
         switch (status) {
         case GpsStatus.GPS_EVENT_FIRST_FIX:
            setSuccess();
            break;
         // _L5:
         case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            Log.d(TAG, "GpsStatus.GPS_EVENT_SATELLITE_STATUS");
            Iterator iterator = m_gpsStatus.getSatellites().iterator();
            int k = 0;
            String s5;
            for (; iterator.hasNext(); Log.d(TAG, s5)) {
               GpsSatellite gpssatellite = (GpsSatellite) iterator.next();
               GpsInfo gpsinfo = mGpsInfo[k];
               int l = gpssatellite.getPrn();
               if (gpsinfo == null) {
                  Log.d(TAG, "mGpsInfo.size------>null");
               }
               gpsinfo.prn = l;
               GpsInfo gpsinfo1 = mGpsInfo[k];
               float f = gpssatellite.getAzimuth();
               gpsinfo1.fAzimuth = f;
               GpsInfo gpsinfo2 = mGpsInfo[k];
               float f1 = gpssatellite.getElevation();
               gpsinfo2.fElevation = f1;
               GpsInfo gpsinfo3 = mGpsInfo[k];
               float f2 = gpssatellite.getSnr();
               gpsinfo3.snr = f2;
               mGpsInfo[k].iID = k;
               k++;
               StringBuilder stringbuilder = (new StringBuilder())
                     .append("mGpsInfo[iCount].prn is ");
               if (mGpsInfo[k] == null) {
                  Log.d(TAG, "mGpsInfo[k]---1--->k" + k);
                  break;
               }
               int i1 = mGpsInfo[k].prn;
               String s1 = stringbuilder.append(i1).toString();
               Log.d(TAG, s1);
               StringBuilder stringbuilder1 = (new StringBuilder())
                     .append("mGpsInfo[iCount].fAzimuth is ");
               float f3 = mGpsInfo[k].fAzimuth;
               String s2 = stringbuilder1.append(f3).toString();
               Log.d(TAG, s2);
               StringBuilder stringbuilder2 = (new StringBuilder())
                     .append("mGpsInfo[iCount].fElevation");
               float f4 = mGpsInfo[k].fElevation;
               String s3 = stringbuilder2.append(f4).toString();
               Log.d(TAG, s3);
               StringBuilder stringbuilder3 = (new StringBuilder())
                     .append("mGpsInfo[iCount].snr");
               float f5 = mGpsInfo[k].snr;
               String s4 = stringbuilder3.append(f5).toString();
               Log.d(TAG, s4);
               StringBuilder stringbuilder4 = (new StringBuilder())
                     .append("mGpsInfo[iCount].iID");
               int j1 = mGpsInfo[k].iID;
               s5 = stringbuilder4.append(j1).toString();
            }

            mStateliteCount = k;
            Log.d(TAG, "mGpsInfo[k]---2--->k" + k);
            StringBuilder stringbuilder5 = (new StringBuilder())
                  .append("the mStateliteCount is");
            int k1 = mStateliteCount;
            String s6 = stringbuilder5.append(k1).toString();
            Log.d(TAG, s6);
            sortStateliteinfo(k);
            Log.d(TAG,"sortsetStateliteinfo--->onGpsStatusChanged1");
            sortsetStateliteinfo(k);
            Log.d(TAG,"sortsetStateliteinfo--->onGpsStatusChanged2");
            // setStateliteinfo(k);
            break;
         // _L2:
         case GpsStatus.GPS_EVENT_STARTED:
            Log.d(TAG, "GpsStatus.GPS_EVENT_STARTED");
            break;
         // _L3:
         case GpsStatus.GPS_EVENT_STOPPED:
            Log.d(TAG, "GpsStatus.GPS_EVENT_STOPPED");
            break;
         /*
          * _L6:
          */
         }
      }

   }

   private class LocaltionLis implements LocationListener {

      public void onLocationChanged(Location location) {
         updateWithNewLocation(location);
      }

      public void onProviderDisabled(String s) {
         updateWithNewLocation(null);
      }

      public void onProviderEnabled(String s) {
      }

      public void onStatusChanged(String s, int i, Bundle bundle) {
      }

   }
   
   @Override
   public boolean dispatchKeyEvent(KeyEvent event) {
       // TODO Auto-generated method stub
       if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
           dissmissPowerClicked();
           Log.d(TAG, " --------->KeyEvent.KEYCODE_VOLUME_DOWN");
           if (powerClicked) {
               Log.d(TAG,
                       "powerClicked --------->KeyEvent.KEYCODE_VOLUME_DOWN");
               testFinish(false, 1, false);
               return true;
           }
           if (tested) {
               Log.d(TAG,
                       "tested --------->KeyEvent.KEYCODE_VOLUME_DOWN");
               testFinish(true, 1, true);
           } else {
               Log.d(TAG, " --------->showTestUncomplete");
               showTestUncomplete();
           }
       } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
           dissmissPowerClicked();
           testFinish(true, -1, true);
       } else if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
           Log.d(TAG, "powerClicked --------->KeyEvent.KEYCODE_POWER");
           powerClicked = true;
           showPowerClicked();
       }
       return true;
   }

   public void storeToFile(String s)
	{
		try
		{	
			Log.d(TAG, "storetoFile------s="+s);
			FileOutputStream outStream = new FileOutputStream("/storage/sdcard0/gpsTest.txt",true);
			OutputStreamWriter writer = new OutputStreamWriter(outStream,"gb2312");
			writer.write(s);
			writer.write("\n");
			writer.flush();
			writer.close();
			outStream.close();
		}
		catch (Exception e)
		{
			Log.d(TAG, "file write error="+e);
		} 
	}

}
